package io.renren.modules.medical.controller;

import io.renren.common.result.Result;
import io.renren.common.utils.PageUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.medical.entity.MedicationAdviceEntity;
import io.renren.modules.medical.service.MedicationAdviceService;
import io.renren.modules.medical.vo.medicalVo.StatusVo;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Map;

/**
 * 医疗建议接口
 * @author luosheng
 * @date 2023-04-04 10:00:52
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/medical/medication/advice")
public class MedicationAdviceController {

    private final MedicationAdviceService medicationAdviceService;

    /**
     * 列表
     */
    @RequiresPermissions("proposal:medicationadvice:list")
    @GetMapping("/list")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params){
        PageUtils page = medicationAdviceService.queryPage(params);
        return Result.success(page);
    }


    /**
     * 信息
     */
    @RequiresPermissions("proposal:medicationadvice:info")
    @GetMapping("/info/{id}")
    public Result<MedicationAdviceEntity> info(@PathVariable("id") Long id){
		MedicationAdviceEntity medicationAdvice = medicationAdviceService.getById(id);
        return Result.success(medicationAdvice);
    }

    /**
     * 保存
     */
    @RequiresPermissions("proposal:medicationadvice:save")
    @PostMapping("/save")
    public Result<String> save(@Validated(AddGroup.class) @RequestBody MedicationAdviceEntity medicationAdvice){
		medicationAdviceService.saveMedicationAdvice(medicationAdvice);
        return Result.success(null);
    }

    /**
     * 修改
     */
    @RequiresPermissions("proposal:medicationadvice:update")
    @PostMapping("/update")
    public Result<String> update(@Validated(UpdateGroup.class) @RequestBody MedicationAdviceEntity medicationAdvice){
		medicationAdviceService.updateById(medicationAdvice);

        return Result.success(null);
    }

    /**
     * 修改状态
     */
    @RequiresPermissions("proposal:medicationadvice:audit")
    @PostMapping("/updateStatus")
    public Result<Object> updateStatus(@RequestBody StatusVo statusVo){
        if (statusVo.getStatus() != 0 && statusVo.getStatus() != 1){
            return Result.fail(null).message("修改状态只能为0或1");
        }
        medicationAdviceService.updateStatus(statusVo);
        return Result.success(null);
    }

    /**
     * 删除
     */
    @RequiresPermissions("proposal:medicationadvice:delete")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody @NotNull Long[] ids){
		medicationAdviceService.removeByIds(Arrays.asList(ids));

        return Result.success(null);
    }

}
